//
//  RoomCardDataProcess.m
//  QRMaster
//
//  Created by myApple on 16/5/23.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "RoomCardDataProcess.h"
#import "BulidingModel.h"
#import "FloorsModel.h"
#import "RoomsModel.h"
#import "CardInfoModel.h"
#import "BulidingInfoLogic.h"
#import "FloorsInfoLogic.h"
#import "RoomInfoLogic.h"

@implementation RoomCardDataProcess

#pragma mark -------解析后台数据成自己想要的类型
-(NSMutableArray *)analysisHouseData:(NSArray *)data
{
    NSMutableArray * cardInfoMutArray = [NSMutableArray array];
    if (data.count > 0)
    {
        for (NSDictionary * bDict in data) {
            BulidingModel * bModel = [[BulidingModel alloc]init];
            bModel.buildingId = [bDict objectForKey:@"buildingid"];
            bModel.buildingName = [bDict objectForKey:@"buildingname"];
            bModel.floors = [bDict objectForKey:@"floors"];
            bModel.itemChildrenArray = [bDict objectForKey:@"floors"];
            bModel.itemId = [bDict objectForKey:@"buildingid"];
            bModel.itemName = [bDict objectForKey:@"buildingname"];
            for (NSDictionary * fDict in bModel.floors) {
                FloorsModel * fModel = [[FloorsModel alloc]init];
                fModel.floorId = [fDict objectForKey:@"floorid"];
                fModel.floorName = [fDict objectForKey:@"floorname"];
                fModel.rooms = [fDict objectForKey:@"rooms"];
                fModel.itemChildrenArray = [bDict objectForKey:@"rooms"];
                fModel.itemId = [bDict objectForKey:@"floorid"];
                fModel.itemName = [bDict objectForKey:@"floorname"];
                for (NSDictionary * rDict in fModel.rooms) {
                    RoomsModel * rModel = [[RoomsModel alloc]init];
                    rModel.roomId = [rDict objectForKey:@"roomid"];
                    rModel.roomName = [rDict objectForKey:@"roomname"];
                    rModel.occupation = [rDict objectForKey:@"occupation"];
                    rModel.lockNo = [rDict objectForKey:@"lockNo"];
                    rModel.isSelect = [rDict objectForKey:@"isSelect"];
                    rModel.roomNo = [rDict objectForKey:@"roomNo"];
                    CardInfoModel * cardModel = [[CardInfoModel alloc]init];
                    cardModel.bulidingName = bModel.buildingName;
                    cardModel.bulidingId = bModel.buildingId;
                    cardModel.floorName = fModel.floorName;
                    cardModel.floorId = fModel.floorId;
                    cardModel.roomName = rModel.roomName;
                    cardModel.roomId = rModel.roomId;
                    cardModel.cardStyle = 2;
                    [cardInfoMutArray addObject:cardModel];
                }
                

            }
        }
    }
    return cardInfoMutArray;
}

#pragma mark ------------插入房间数据到数据库
-(void)insertDataIntoRooms:(NSArray *)allBuildingInfo
{
    BulidingInfoLogic * bulidingDataBase = [[BulidingInfoLogic alloc]init];
    FloorsInfoLogic * floorDataBase = [[FloorsInfoLogic alloc]init];
    RoomInfoLogic * roomDataBase = [[RoomInfoLogic alloc]init];
    for (NSDictionary * bDict in allBuildingInfo)
    {
        BulidingModel * bModel = [[BulidingModel alloc]init];
        bModel.buildingName = [bDict objectForKey:@"buildingname"];
        bModel.buildingId = [bDict objectForKey:@"buildingid"];
        bModel.buildingNo = [bDict objectForKey:@"buildingno"];
        [bulidingDataBase insertBulidingInfo:bModel withCommunityId:[QRSingleton sharedQRSingleton].communityId];
        bModel.floors = [bDict objectForKey:@"floors"];
        for (NSDictionary * fDict in bModel.floors) {
            FloorsModel * fModel = [[FloorsModel alloc]init];
            fModel.floorId = [fDict objectForKey:@"floorid"];
            fModel.floorName = [fDict objectForKey:@"floorname"];
            fModel.floorNo  = [fDict objectForKey:@"floorno"];
            [floorDataBase insertFloorInfo:fModel withCommunityId:[QRSingleton sharedQRSingleton].communityId buildingId:bModel.buildingId];
            fModel.rooms = [fDict objectForKey:@"rooms"];
            for (NSDictionary * rDict in fModel.rooms) {
                RoomsModel * rModel = [[RoomsModel alloc]init];
                rModel.roomId = [rDict objectForKey:@"roomid"];
                rModel.roomName = [rDict objectForKey:@"roomname"];
                rModel.roomNo = [rDict objectForKey:@"roomno"];
                rModel.isSelect = [rDict objectForKey:@"isSelected"];
                rModel.lockNo = [rDict objectForKey:@"lockno"];
                rModel.occupation = [rDict objectForKey:@"occupation"];
                [roomDataBase insertRoomInfo:rModel withFloorId:fModel.floorId withCommunityId:[QRSingleton sharedQRSingleton].communityId];
            }
        }
    }
}


#pragma mark ---------从缓存中读取房间数据
-(void)getDataFromDisk
{
    BulidingInfoLogic * bulidingDataBase = [[BulidingInfoLogic alloc]init];
    FloorsInfoLogic * floorDataBase = [[FloorsInfoLogic alloc]init];
    RoomInfoLogic * roomDataBase = [[RoomInfoLogic alloc]init];
    NSMutableArray * cardInfoMutArray = [[NSMutableArray alloc]init];
    NSMutableArray * buildingArray = [[NSMutableArray alloc]init];
    [bulidingDataBase queryBulidingInfo:[QRSingleton sharedQRSingleton].communityId block:^(NSMutableArray *bulidingArray)
     {
         for (BulidingModel * bModel in bulidingArray) {
             
             [floorDataBase queryFloorsInfo:bModel.buildingId block:^(NSMutableArray *arr)
              {
                  bModel.itemChildrenArray = arr;
                  for (FloorsModel * fModel in arr) {
                      [roomDataBase queryRoomInfo:fModel.floorId block:^(NSMutableArray * roomResultArray)
                       {
                           fModel.itemChildrenArray = roomResultArray;
                           for (RoomsModel * model in roomResultArray)
                           {
                               CardInfoModel * cardModel = [[CardInfoModel alloc]init];
                               cardModel.bulidingId = bModel.buildingId;
                               cardModel.bulidingName = bModel.buildingName;
                               cardModel.floorId = fModel.floorId;
                               cardModel.floorName = fModel.floorName;
                               cardModel.roomId = model.roomId;
                               cardModel.roomName = model.roomName;
                               [cardInfoMutArray addObject:cardModel];
                           }
                       }];
                      
                  }
              }];
             [buildingArray addObject:bModel];
         }
         
     }];
}

#pragma mark ----------从缓存中删除相应的房间
-(void)deleteRooms
{
    BulidingInfoLogic * bulidingDataBase = [[BulidingInfoLogic alloc]init];
    [bulidingDataBase deleteBulidingWithCommunityId:[QRSingleton sharedQRSingleton].communityId];
    FloorsInfoLogic * floorDataBase = [[FloorsInfoLogic alloc]init];
    [floorDataBase deleteFloorsInfoWithCommunityId:[QRSingleton sharedQRSingleton].communityId];
    RoomInfoLogic * roomDataBase = [[RoomInfoLogic alloc]init];
    [roomDataBase deleteRoomWithCommunityId:[QRSingleton sharedQRSingleton].communityId];
}



@end
